package org.jivesoftware.smackx.muc;

import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;

class RoomListenerMultiplexor
  implements ConnectionListener
{
  private static final Map<Connection, WeakReference<RoomListenerMultiplexor>> monitors = new WeakHashMap();
  private Connection connection;
  private RoomMultiplexFilter filter;
  private RoomMultiplexListener listener;

  public static RoomListenerMultiplexor getRoomMultiplexor(Connection paramConnection)
  {
    synchronized (monitors)
    {
      if ((!monitors.containsKey(paramConnection)) || (((WeakReference)monitors.get(paramConnection)).get() == null))
      {
        RoomListenerMultiplexor localRoomListenerMultiplexor = new RoomListenerMultiplexor(paramConnection, new RoomMultiplexFilter(null), new RoomMultiplexListener(null));
        localRoomListenerMultiplexor.init();
        monitors.put(paramConnection, new WeakReference(localRoomListenerMultiplexor));
      }
      return (RoomListenerMultiplexor)((WeakReference)monitors.get(paramConnection)).get();
    }
  }

  private RoomListenerMultiplexor(Connection paramConnection, RoomMultiplexFilter paramRoomMultiplexFilter, RoomMultiplexListener paramRoomMultiplexListener)
  {
    if (paramConnection == null)
      throw new IllegalArgumentException("Connection is null");
    if (paramRoomMultiplexFilter == null)
      throw new IllegalArgumentException("Filter is null");
    if (paramRoomMultiplexListener == null)
      throw new IllegalArgumentException("Listener is null");
    this.connection = paramConnection;
    this.filter = paramRoomMultiplexFilter;
    this.listener = paramRoomMultiplexListener;
  }

  public void addRoom(String paramString, PacketMultiplexListener paramPacketMultiplexListener)
  {
    this.filter.addRoom(paramString);
    this.listener.addRoom(paramString, paramPacketMultiplexListener);
  }

  public void connectionClosed()
  {
    cancel();
  }

  public void connectionClosedOnError(Exception paramException)
  {
    cancel();
  }

  public void reconnectingIn(int paramInt)
  {
  }

  public void reconnectionSuccessful()
  {
  }

  public void reconnectionFailed(Exception paramException)
  {
  }

  public void init()
  {
    this.connection.addConnectionListener(this);
    this.connection.addPacketListener(this.listener, this.filter);
  }

  public void removeRoom(String paramString)
  {
    this.filter.removeRoom(paramString);
    this.listener.removeRoom(paramString);
  }

  private void cancel()
  {
    this.connection.removeConnectionListener(this);
    this.connection.removePacketListener(this.listener);
  }

  private static class RoomMultiplexFilter
    implements PacketFilter
  {
    private Map<String, String> roomAddressTable = new ConcurrentHashMap();

    public boolean accept(Packet paramPacket)
    {
      String str = paramPacket.getFrom();
      if (str == null)
        return false;
      return this.roomAddressTable.containsKey(StringUtils.parseBareAddress(str).toLowerCase());
    }

    public void addRoom(String paramString)
    {
      if (paramString == null)
        return;
      this.roomAddressTable.put(paramString.toLowerCase(), paramString);
    }

    public void removeRoom(String paramString)
    {
      if (paramString == null)
        return;
      this.roomAddressTable.remove(paramString.toLowerCase());
    }
  }

  private static class RoomMultiplexListener
    implements PacketListener
  {
    private Map<String, PacketMultiplexListener> roomListenersByAddress = new ConcurrentHashMap();

    public void processPacket(Packet paramPacket)
    {
      String str = paramPacket.getFrom();
      if (str == null)
        return;
      PacketMultiplexListener localPacketMultiplexListener = (PacketMultiplexListener)this.roomListenersByAddress.get(StringUtils.parseBareAddress(str).toLowerCase());
      if (localPacketMultiplexListener != null)
        localPacketMultiplexListener.processPacket(paramPacket);
    }

    public void addRoom(String paramString, PacketMultiplexListener paramPacketMultiplexListener)
    {
      if (paramString == null)
        return;
      this.roomListenersByAddress.put(paramString.toLowerCase(), paramPacketMultiplexListener);
    }

    public void removeRoom(String paramString)
    {
      if (paramString == null)
        return;
      this.roomListenersByAddress.remove(paramString.toLowerCase());
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smackx.muc.RoomListenerMultiplexor
 * JD-Core Version:    0.6.1
 */